<template>
  <div class="home-category">
    <ul class="menu">
      <li v-for="item in categoryList" :key="item.id">
        <RouterLink :to="item.id ? `/category/${item.id}` : '/'">{{ item.name }}</RouterLink>
        <a v-for="i in item.children" :key="i.id" href="javascript:;">{{ i.name }}</a>
        <!-- 骨架效果 -->
        <template v-if="!item.children">
          <XtxSkeleton width="70px" height="18px" bg="rgba(255,255,255,0.2)" />
          <XtxSkeleton width="40px" height="18px" bg="rgba(255,255,255,0.2)" />
        </template>
        <!-- 弹层 -->
        <div class="layer">
          <h4>{{ item.brands ? '品牌' : '分类' }}推荐 <small>根据您的购买或浏览记录推荐</small></h4>
          <ul v-if="item.brands">
            <li class="brand" v-for="bran in item.brands" :key="bran.name">
              <RouterLink to="/">
                <img :src="bran.picture" alt="" />
                <div class="info">
                  <p class="place"><i class="iconfont icon-dingwei"></i>{{ bran.place }}</p>
                  <p class="name ellipsis">{{ bran.name }}</p>
                  <p class="desc ellipsis-2">{{ bran.desc }}</p>
                </div>
              </RouterLink>
            </li>
          </ul>
          <ul v-else>
            <li v-for="sub in item.goods" :key="sub.id">
              <RouterLink :to="`/goods/${sub.id}`">
                <img :src="sub.picture" alt="" />
                <div class="info">
                  <p class="name ellipsis-2">{{ sub.name }}</p>
                  <p class="desc ellipsis">{{ sub.desc }}</p>
                  <p class="price"><i>¥</i>{{ sub.price }}</p>
                </div>
              </RouterLink>
            </li>
          </ul>
        </div>
      </li>
    </ul>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { computed, reactive, onMounted } from 'vue'
import { getBrand } from '@/api/home.js'
export default {
  name: 'HomeCategory',
  setup() {
    const store = useStore()
    // 1.现有的计算属性categoryList中没有品牌分类，定义品牌属性（响应式）
    // 2.计算属性同时依赖品牌数据的到10长度的渲染数据
    // 3.定义api函数，组件调用获取数据
    // 4.加入品牌结构和样式渲染即可
    const brand = reactive({
      // 不写id是因为和分类的跳转路径不一样，不需要id
      name: '品牌',
      children: [{ name: '品牌推荐' }],
      brands: [] // 请求后台添加数据
    })
    onMounted(async () => {
      const data = await getBrand(6)
      if (data.code === '1') {
        brand.brands = data.result.slice(0, 6)
      }
    })
    // 1.渲染
    // 1.1从vuex中拿取数据，
    // 1.2利用计算属性和map的到需要渲染的数组
    // 分类列表是异步获取的，所以的用计算属性
    const categoryList = computed(() => {
      const list = store.state.category.category.map(item => {
        // 保留children数组中的前两项，利用slice切割数组
        const { id, name, goods, children } = item
        return { id, name, goods, children: children && children.slice(0, 2) }
      })
      list.push(brand) // 添加品牌数据
      return list
    })
    return { categoryList }
  }
}
</script>

<style scoped lang="less">
.xtx-skeleton {
  animation: fade 1.5s linear infinite alternate;
  margin-right: 6px;
}
@keyframes fade {
  from {
    opacity: 0.2;
  }
  to {
    opacity: 1;
  }
}
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .menu {
    > li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &:hover {
        background: var(--xtx-color);
      }
      .layer {
        width: 990px;
        height: 500px;
        background: rgba(255, 255, 255, 0.8);
        position: absolute;
        left: 250px;
        top: 0;
        display: none;
        padding: 0 15px;
        h4 {
          font-size: 20px;
          font-weight: normal;
          line-height: 80px;
          small {
            font-size: 16px;
            color: #666;
          }
        }
        ul {
          display: flex;
          flex-wrap: wrap;
          li {
            width: 310px;
            height: 120px;
            margin-right: 15px;
            margin-bottom: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
            background: #fff;
            &:nth-child(3n) {
              margin-right: 0;
            }
            a {
              display: flex;
              width: 100%;
              height: 100%;
              align-items: center;
              padding: 10px;
              &:hover {
                background: #e3f9f4;
              }
              img {
                width: 95px;
                height: 95px;
              }
              .info {
                padding-left: 10px;
                line-height: 24px;
                width: 190px;
                .name {
                  font-size: 16px;
                  color: #666;
                }
                .desc {
                  color: #999;
                }
                .price {
                  font-size: 22px;
                  color: var(--price-color);
                  i {
                    font-size: 16px;
                  }
                }
              }
            }
          }
          li.brand {
            height: 180px;
            a {
              align-items: flex-start;
              img {
                width: 120px;
                height: 160px;
              }
              .info {
                p {
                  margin-top: 8px;
                }
                .place {
                  color: #999;
                }
              }
            }
          }
        }
      }
      &:hover {
        .layer {
          display: block;
        }
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
}
</style>
